package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.R;
import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.RatingBar;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.UserInfoUtils;
import com.diudiutechnology.www.yadiudiu.util.view.SildingFinishLayout;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.BusinessCenterModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MerchantsTypeModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MessageActivityModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MineFragmentModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.RegistrationModel;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/23.
 * 商家中心
 */

public class BusinessCenterActivity extends Activity implements View.OnClickListener, Callback.CommonCallback<String> {
    @Bind(R.id.retur)
    LinearLayout retur;

    @Bind(R.id.work)
    LinearLayout work;
    @Bind(R.id.work_text)//3 打烊、2营业中
            TextView work_text;
    @Bind(R.id.work_img)
    ImageView work_img;

    @Bind(R.id.today_the_order)//今日订单
            TextView today_the_order;

    @Bind(R.id.income)//今日收入
            TextView income;

    @Bind(R.id.layout_store_the_data)//店铺资料
            RelativeLayout layout_store_the_data;

    @Bind(R.id.layout_management)//商品管理
            RelativeLayout layout_management;

    @Bind(R.id.waiting_layout)//待接单
            RelativeLayout waiting_layout;

    @Bind(R.id.shopCartNum)//待接单数量
            TextView shopCartNum;

    @Bind(R.id.have_order_layout)//已接单
            RelativeLayout have_order_layout;
    @Bind(R.id.have_order_text)//已接单数量
            TextView have_order_text;


    @Bind(R.id.completed_order_layout)//已完成
            RelativeLayout completed_order_layout;
    @Bind(R.id.completed_order_text)//已完成数量
            TextView completed_order_text;


    @Bind(R.id.texttitle_right)//消息
            RelativeLayout texttitle_right;

    @Bind(R.id.layout_taskcenter)//余额
            RelativeLayout layout_taskcenter;

    @Bind(R.id.balance_money)//我的余额
            TextView balance_money;

    @Bind(R.id.layout_service)//客服中心
            RelativeLayout layout_service;


    @Bind(R.id.layout_reviews)//店铺评论
            RelativeLayout layout_reviews;

    @Bind(R.id.red_packets)//店铺评论条数
            TextView red_packets;


    @Bind(R.id.layout_turnover)//营业额
            RelativeLayout layout_turnover;

    @Bind(R.id.turnover_money)//营业额钱
            TextView turnover_money;

    @Bind(R.id.gexing_wei)//消息
            TextView gexing_wei;


    @Bind(R.id.promote)//推荐用户人数
            TextView promote;

    @Bind(R.id.rider)//推荐骑手人数
            TextView rider;

    @Bind(R.id.are_rated)//店铺名称
            TextView are_rated;

    @Bind(R.id.rating)//店铺评星
            RatingBar rating;

    @Bind(R.id.automatic_layou)
    LinearLayout automatic_layou;
    @Bind(R.id.automatic)//是否开启自动接单
            TextView automatic;
//
    @Bind(R.id.scrollView)
ScrollView scrollView;
    @Bind(R.id.sildingFinishLayout)
    SildingFinishLayout sildingFinishLayout;
    Context context;
    String merchanttype = "";//类型
    List<BusinessCenterModel> businessmerchant = new ArrayList<>();//merchant数据
    private MyProgressDialog mDialog;
    String Judge_interface = "0";//判断哪个接口   0、获取数据 1、消息 2、打样和上班
    String ison = "2";//3为打烊 2为上班
    String autom = "2";//2为手动接单3为自动接单
String u_id="";
    private View viewkre;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewkre = View.inflate(this, R.layout.activity_business_center, null);
        setContentView(viewkre);
        ButterKnife.bind(this);

        context = this;
        initView();
//        // touchView要设置到ListView上面
//        sildingFinishLayout.setTouchView(scrollView);
    }

    private void initView() {
        retur.setOnClickListener(this);
        layout_store_the_data.setOnClickListener(this);
        layout_management.setOnClickListener(this);
        completed_order_layout.setOnClickListener(this);
        have_order_layout.setOnClickListener(this);
        waiting_layout.setOnClickListener(this);
        texttitle_right.setOnClickListener(this);
        layout_taskcenter.setOnClickListener(this);
        layout_service.setOnClickListener(this);
        layout_reviews.setOnClickListener(this);
        work.setOnClickListener(this);
        automatic_layou.setOnClickListener(this);
        initData();
//        sildingFinishLayout.setOnSildingFinishListener(new SildingFinishLayout.OnSildingFinishListener() {
//
//            @Override
//            public void onSildingFinish() {
//                BusinessCenterActivity.this.finish();
//            }
//        });
    }




    //绑定逻辑
    private void initData() {
        MineFragmentModel mineFragmentModel = UserInfoUtils.readUserInfo(this);
        merchanttype = mineFragmentModel.getMerchanttype();
        if (merchanttype.equals("1")) {//买房商家
            //营业额隐藏
            layout_turnover.setVisibility(View.GONE);
        } else if (merchanttype.equals("2")) {//购车商家
            //营业额隐藏
            layout_turnover.setVisibility(View.GONE);
        } else if (merchanttype.equals("3")) {//干洗商家

        } else if (merchanttype.equals("4")) {//开锁商家
            //营业额隐藏
            layout_turnover.setVisibility(View.GONE);
            //商品管理
            layout_management.setVisibility(View.GONE);
        } else if (merchanttype.equals("5")) {//果蔬商家

        } else if (merchanttype.equals("6")) {//苦力商家
            //营业额隐藏
            layout_turnover.setVisibility(View.GONE);
            //商品管理
            layout_management.setVisibility(View.GONE);
        } else if (merchanttype.equals("7")) {//快递商家

        } else if (merchanttype.equals("8")) {//家政商家

        } else if (merchanttype.equals("9")) {//洗车商家

        }
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.work://打样
                processproofing();
                break;
            case R.id.automatic_layou://打样
                processautomaticorder();
                break;


            case R.id.layout_store_the_data://店铺资料
                intent.setClass(context, StoreTheDataActivity.class);
                intent.putExtra("id", u_id);
                startActivity(intent);
                break;
            case R.id.layout_management://商品管理
                intent.setClass(context, CommodityManagementActivity.class);
                intent.putExtra("id",u_id);
                startActivity(intent);

                break;
            case R.id.waiting_layout://待接单
                intent.setClass(context, WaitingActivity.class);
                intent.putExtra("pos", "0");
                intent.putExtra("id",u_id);
                startActivity(intent);

                break;
            case R.id.have_order_layout://已接单
                intent.setClass(context, WaitingActivity.class);
                intent.putExtra("pos", "1");
                intent.putExtra("id",u_id);
                startActivity(intent);

                break;
            case R.id.completed_order_layout://已完成
                intent.setClass(context, WaitingActivity.class);
                intent.putExtra("pos", "2");
                intent.putExtra("id",u_id);
                startActivity(intent);

                break;
            case R.id.texttitle_right://消息
                intent.setClass(context, MessageActivity.class);
                startActivity(intent);
                break;
            case R.id.layout_taskcenter://余额
                intent.setClass(context, TheBalanceOfMyActivity.class);
                startActivity(intent);
                break;
            case R.id.layout_service://客服
                intent.setClass(context, CustomerServiceCenterActivity.class);
                startActivity(intent);
                break;
            case R.id.layout_reviews://店铺评论
                intent.setClass(context, AllEvaluationActivity.class);
                intent.putExtra("pos","0");
                intent.putExtra("merchantid",u_id);//店铺id
                intent.putExtra("merchanttype",merchanttype);//商家类型
                startActivity(intent);
                break;
            //AddTheServiceActivity 添加家政服务
            //CarAddGoodsActivity 添加洗车 商品
            //AddCarPurchasesGoodsActivity 添加购车 商品
            //AddThePropertyActivity 添加楼盘 买房
            //FamilyManagementActivity 户型管理
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        processinitData();
    }

    //初始数据
    private void processinitData() {
        if (Sundry.isOpenNetwork(context)) {
            mDialog = new MyProgressDialog(context);
            Judge_interface = "0";
            String Data="";
            if (merchanttype.equals("1")) {//买房商家
                Data= Constant.MERCHANTCENTREHOUSING;

            } else if (merchanttype.equals("2")) {//购车商家
                Data= Constant.MERCHANTCENTRECARBUYING;

            } else if (merchanttype.equals("3")) {//干洗商家
                Data= Constant.MERCHANTCENTRECLEANING;

            } else if (merchanttype.equals("4")) {//开锁商家
                Data= Constant.MERCHANTCENTREUNLOCKING;

            } else if (merchanttype.equals("5")) {//果蔬商家
                Data= Constant.MERCHANTCENTREVEGETABLES;

            } else if (merchanttype.equals("6")) {//苦力商家
                Data= Constant.MERCHANTCENTRECOOLIE;

            } else if (merchanttype.equals("7")) {//快递商家

            } else if (merchanttype.equals("8")) {//家政商家
                Data= Constant.MERCHANTCENTREHOUSEKEEPING;

            } else if (merchanttype.equals("9")) {//洗车商家
                Data= Constant.MERCHANTCENTRECARWASH;

            }
            HttpClient.cleaning(Data,this);
        } else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }

    //消息中心
    private void processMessages() {
        Judge_interface = "1";
        HttpClient.Messages(this);
    }

    //打样和上班
    private void processproofing() {
        if (Sundry.isOpenNetwork(context)) {
            mDialog = new MyProgressDialog(context);
            Judge_interface = "2";
            HttpClient.proofing(u_id, ison, this);
        } else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
    //开启和取消自动接单
    private void processautomaticorder() {
        if (Sundry.isOpenNetwork(context)) {
            mDialog = new MyProgressDialog(context);
            Judge_interface = "3";
            HttpClient.automaticorder(u_id, autom, this);
        } else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
    @Override
    public void onSuccess(String result) {
        System.out.println("stringmd5result"+result);
        if (Judge_interface.equals("1")) {//消息
            List<RegistrationModel> getnote = JsonTools.getunderlying(result);
            if (getnote != null || getnote.size() > 0) {
                String status = getnote.get(0).getStatus();
                String getMsg = getnote.get(0).getMsg();
                if (status.equals("200")) {
                    String content = getnote.get(0).getContent();
                    List<MessageActivityModel> message = JsonTools.getMessage(content);
                    if (message != null || message.size() > 0) {
                        String mes = "";
                        for (int i = 0; i < message.size(); i++) {
                            if (mes.equals("")) {
                                mes = message.get(i).getMsgstatus();
                            } else {
                                mes = mes + "," + message.get(i).getMsgstatus();
                            }
                        }
                        if (mes.indexOf("2") != -1) {
                            gexing_wei.setVisibility(View.VISIBLE);
                        } else {
                            gexing_wei.setVisibility(View.INVISIBLE);
                        }
                    }
                } else {
                    gexing_wei.setVisibility(View.INVISIBLE);
                }
            }
        } else {//获取数据
            processaccessdata(result);
        }
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        if (!Judge_interface.equals("1")) {
            if (mDialog != null) {
                mDialog.dismiss();
            }
            ToastUtils.Toastshow(context, getResources().getString(R.string.server));
        }

    }

    @Override
    public void onCancelled(CancelledException cex) {
        if (!Judge_interface.equals("1")) {
            if (mDialog != null) {
                mDialog.dismiss();
            }
            ToastUtils.Toastshow(context, getResources().getString(R.string.server));
        }

    }

    @Override
    public void onFinished() {

    }

    //获取数据
    private void processaccessdata(String result) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        List<MerchantsTypeModel> getnote = JsonTools.getmerchanttype(result);
        if (getnote != null || getnote.size() > 0) {
            String error = getnote.get(0).getError();
            String mssage = getnote.get(0).getMssage();
            if (error.equals("200")) {
                String content = getnote.get(0).getResult();
                if (Judge_interface.equals("0")) {//商家中心数据
                    List<BusinessCenterModel> business = JsonTools.getBusiness(content);
                    if (business != null || business.size() > 0) {
                        processdata(business);
                    }
                    processMessages();
                } else if (Judge_interface.equals("2")) {//打样和上班
                    if (ison.equals("2")) {
                        work_text.setText("营业中");
                        work_img.setVisibility(View.VISIBLE);
                        ison = "3";
                    } else {
                        work_text.setText("休息中");
                        work_img.setVisibility(View.GONE);
                        ison = "2";
                    }
                }else if (Judge_interface.equals("3")) {//开启和取消自动接单
                    if (autom.equals("2")) {
                        automatic.setText(getResources().getString(R.string.automatic));
                        autom = "3";
                    } else {
                        automatic.setText(getResources().getString(R.string.automatic1));
                        autom = "2";
                    }
                }
            } else if (error.equals("105")) {
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
                ToastUtils.show(context, mssage);
            } else {
                ToastUtils.show(context, mssage);
            }
        }

    }

    //绑定数据
    private void processdata(List<BusinessCenterModel> business) {
        businessmerchant = JsonTools.getBusinessmerchant(business.get(0).getMerchant());
        if (businessmerchant != null || businessmerchant.size() > 0) {
            are_rated.setText(businessmerchant.get(0).getName());
            rating.setStar(Float.valueOf(businessmerchant.get(0).getStar()));
            u_id=businessmerchant.get(0).getId();
            if (businessmerchant.get(0).getIson().equals("2")) {//2营业中
                work_text.setText("营业中");
                work_img.setVisibility(View.VISIBLE);
                ison = "3";
            } else {//3打烊
                work_text.setText("休息中");
                work_img.setVisibility(View.GONE);
                ison = "2";
            }

            if (businessmerchant.get(0).getAutomatic().equals("2")) {//2为等待接单
                automatic.setText(getResources().getString(R.string.automatic));
                autom="3";
            } else {//3为自动接单
                autom="2";
                automatic.setText(getResources().getString(R.string.automatic1));
            }
        }
        if (business.get(0).getWjcorder().equals("0")) {//待接单数量
            shopCartNum.setVisibility(View.GONE);
        } else {
            shopCartNum.setVisibility(View.VISIBLE);
            shopCartNum.setText(business.get(0).getWjcorder());
        }
        if (business.get(0).getYjorder().equals("0")) {//已接单数量
            have_order_text.setVisibility(View.GONE);
        } else {
            have_order_text.setVisibility(View.VISIBLE);
            have_order_text.setText(business.get(0).getYjorder());
        }

        if (business.get(0).getWcorder().equals("0")) {//已完成数量
            completed_order_text.setVisibility(View.GONE);
        } else {
            completed_order_text.setVisibility(View.VISIBLE);
            completed_order_text.setText(business.get(0).getWcorder());
        }

        today_the_order.setText(business.get(0).getCounttodayincome());//今日订单
        income.setText(business.get(0).getTodayincome());//今日收入
        turnover_money.setText(business.get(0).getTotalmoney() + "元");//营业额
        balance_money.setText(business.get(0).getUsermoney() + "元");//我的余额
        red_packets.setText(business.get(0).getShopcomment());//评论
        promote.setText(business.get(0).getTgcountuser() + "人");//推荐用户人数
        rider.setText(business.get(0).getHorseman() + "人");//推荐骑手人数
    }
}
